import {createSSRApp, nextTick} from "vue";
import {createRouter} from "./router";
import {createStore} from "./store";
import './style/app.css'
import App from "./App.vue";
import markdown from "./utils/markdown";
import {fileGet} from "./api/sys";
import { useAntd } from "./antdv";

export function createApp() {
    const app = createSSRApp(App);
    const router = createRouter();
    const store = createStore();

    app.use(router)
    app.use(store)
    useAntd(app)

    app.mixin({
        beforeMount() {
            const {asyncData} = this.$options;
            if (asyncData) {
                // 将获取数据操作分配给 promise
                // 以便在组件中，我们可以在数据准备就绪后
                // 通过运⾏ `this.dataPromise.then(...)` 来执⾏其他任务
                this.dataPromise = asyncData({
                    store: this.$store,
                    router: this.$route,
                });
                if(this.$route.path.startsWith('/article')){
                    if(!store.state.postInfo.title && !store.state.postInfo.contex){
                        this.$router.push('/404')
                    }
                    this.dataPromise.then(async ()=> {
                        if (store.state.postInfo.context) {
                            store.state.postInfo.context = markdown.marked(store.state.postInfo.context);
                        }
                        if (!store.state.postInfo.cover) {
                            store.state.postInfo.cover = await fileGet(1)
                        }
                       await nextTick(function() {
                            markdown.toView()
                        })
                    })
                }
            }
        }
    })

    return {app, router, store};
}